package nit.wave.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import nit.wave.model.Student;
import nit.wave.utils.JdbcUtils;

public class DataBaseDAO {

	public static int findUserByOpenId(String tableName, String openId) throws SQLException {
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		int result = 0;
		try {
			conn = JdbcUtils.getConnection();
			String sql = "select * from " + tableName + " where openID ='" + openId + "'";
			ps = conn.prepareStatement(sql);
			rs = ps.executeQuery();
			if (rs.next()) {
				return 1;
			}
		} finally {
			JdbcUtils.free(rs, ps, conn);
		}
		return result;
	}

	public static int findUserByuSserId(String tableName, String userId) throws SQLException {
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		int result = 0;
		try {
			conn = JdbcUtils.getConnection();
			String sql = "select * from " + tableName + " where uid ='" + userId + "'";
			ps = conn.prepareStatement(sql);
			rs = ps.executeQuery();
			if (rs.next()) {
				return 1;
			}
		} finally {
			JdbcUtils.free(rs, ps, conn);
		}
		return result;
	}

	public static Student getStudent(String tableName, String openId) throws SQLException {
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		Student student = null ;
		try {
			conn = JdbcUtils.getConnection();
			String sql = "select * from " + tableName + " where openID = '" + openId + "'";
			ps = conn.prepareStatement(sql);
			rs = ps.executeQuery();
			if (rs.next()) {
				student = new Student();
				student.setOpenId(openId);
				student.setUserId(rs.getString("uid"));
				student.setPassword(rs.getString("password"));
			}
			return student;
		} finally {
			JdbcUtils.free(rs, ps, conn);
		}
	}

	public static boolean intsertUser(String tableName, Student student) throws Exception {
		boolean result = false;
		Connection conn = null;
		PreparedStatement ps = null;
		int rs = 0;
		try {
			conn = JdbcUtils.getConnection();
			String sql = "insert into " + tableName + "(openID, uid, password,time) values ( '"+student.getOpenId()+"','"+student.getUserId()+ "','"+student.getPassword()+"', UNIX_TIMESTAMP())";
			ps = (PreparedStatement) conn.prepareStatement(sql);
			ps = conn.prepareStatement(sql);
			rs = ps.executeUpdate();
			if (rs >= 1) {
				return true;
			}
		} finally {
			JdbcUtils.free(null, ps, conn);
		}
		return result;
	}

	public static void updatePassword(String tableName ,String openId, String password) {
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			conn = JdbcUtils.getConnection();
			String sql = "update "+tableName+" set password=? where openID=?";
			ps = (PreparedStatement) conn.prepareStatement(sql);
			ps.setString(1, password);
			ps.setString(2, openId);
			ps.executeUpdate();
			
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			JdbcUtils.free(rs, ps, conn);
		}
	}

}
